// pages/order/orderDetail/orderDetail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderId: null,
        orderInfo: null,
        dignity: null,
        roleList: ['无', '普通会员', '会员']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.dignity) {
            this.setData({
                dignity: options.dignity
            })
        }
        this.setData({
            orderId: options.orderId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getOrderInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getOrderInfo: function() {
        const that = this
        let orderInfo = this.data.orderInfo
        let data = {
            order_id: this.data.orderId,
            get_user: 1,
            paramter: 1
        }
        common.post('Orderlist/user_order', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    orderInfo: orderInfo
                })
            } else {
                orderInfo = res[0]
                that.setData({
                    orderInfo: orderInfo
                })
            }
        }).catch(function(e) {})
    },

    goLogistics: function(e) {
        wx.navigateTo({
            url: '/pages/order/orderLogistics/orderLogistics?expressId=' + e.currentTarget.dataset.id
        })
    },

    goCancle: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认取消订单？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    let data = {
                        order_id: e.currentTarget.dataset.id
                    }
                    common.post('Orderlist/cancel_order', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '成功取消订单',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.redirectTo({
                                        url: '/pages/order/orderCenter/orderCenter?orderType=5'
                                    })
                                }
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    },

    confirmOrder: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '是否确认收货？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    let data = {
                        order_id: e.currentTarget.dataset.id
                    }
                    common.post('Orderlist/affirm_order', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '成功确认收货',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.redirectTo({
                                        url: '/pages/order/orderCenter/orderCenter?orderType=4'
                                    })
                                }
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    },

    goComment: function(e) {
        wx.navigateTo({
            url: '/pages/order/orderComment/orderComment?orderId=' + e.currentTarget.dataset.id
        })
    },

    goRefund: function(e) {
        wx.navigateTo({
            url: '/pages/order/refund/refund?orderId=' + e.currentTarget.dataset.id
        })
    },

    goPay: function(e) {
        wx.login({
            success(respone) {
                if (respone.code) {
                    //发起网络请求
                    wx.showLoading({
                        title: '支付中'
                    })
                    let data = {
                        order_sn: e.currentTarget.dataset.sn,
                        code: respone.code,
                        pay_client: 1
                    }
                    common.post('Orderlist/sm_again_pay_order', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) { }
                            })
                            return
                        }
                        wx.requestPayment({
                            timeStamp: res.timeStamp,
                            nonceStr: res.nonceStr,
                            package: res.package,
                            signType: res.signType,
                            paySign: res.paySign,
                            success(res) {
                                wx.redirectTo({
                                    url: '/pages/order/orderCenter/orderCenter?orderType=2'
                                })
                            },
                            fail(res) {}
                        })
                    }).catch(function(e) {})
                }
            }
        })
    },

    phoneCall: function(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    }
})